define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id'), visible: false},
                        {field: 'user.nickname', title: __('nickname'), operate: 'LIKE'},
                        {field: 'seller_id', title: __('商家ID'), visible: false},
                        {field: 'seller.name', title: __('Seller_id'), operate: 'LIKE'},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'order_type', title: __('Order_type'), visible: false, searchList: Config.orderTypeList},
                        {field: 'order_type_text', title: __('Order_type'), operate: false},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'goods_amount', title: __('Goods_amount'), operate:'BETWEEN'},
                        {field: 'pay_amount', title: __('Pay_amount'), operate:'BETWEEN'},
                        {field: 'number', title: __('Number')},
                        {field: 'status', title: __('Status'), searchList: Config.statusList, visible: false},
                        {field: 'status_text', title: __('Status'), operate: false, formatter: Table.api.formatter.status, custom:{'正常':'success'}},
                        {field: 'pay_status', title: __('Pay_status'), searchList: Config.payStatusList, visible: false},
                        {field: 'pay_status_text', title: __('Pay_status'), operate: false, formatter: Table.api.formatter.status, custom:{'正常':'success'}},
                        // {field: 'pay_type', title: __('Pay_type'), searchList: Config.payTypList, visible: false},
                        // {field: 'pay_type_text', title: __('Pay_type'), operate: false, formatter: Table.api.formatter.status, custom:{'正常':'success'}},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'shipping_status', title: __('Shipping_status'), searchList: Config.shippingStatusList, visible: false},
                        {field: 'shipping_status_text', title: __('Shipping_status'), operate: false,  formatter: Table.api.formatter.status},
                        // {field: 'shipping_time', title: __('Shipping_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'sign_time', title: __('Sign_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'remark', title: __('Remark')},
                        {field: 'transaction_no', title: __('Transaction_no')},
                        {field: 'recommend.nickname', title: __('Recommend_user_id'), operate: 'LIKE'},
                        // {field: 'close_type', title: __('Close_type'), searchList: Config.closeTypeList, visible: false},
                        // {field: 'close_type_text', title: __('Close_type'), operate: false},
                        // {field: 'comment_status', title: __('Comment_status'), searchList: Config.commentStatusList, visible: false},
                        // {field: 'comment_status_text', title: __('Comment_status'), operate: false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'refund', 
                                    text: '退款', 
                                    title: '退款', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-danger btn-ajax', 
                                    confirm: '确认退回该订单金额吗？', 
                                    url: 'order/refund',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                                {
                                    name: 'shipping', 
                                    text: '发货', 
                                    title: '发货', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-info btn-dialog', 
                                    url: function(row, column){
                                        return 'order_shipping/add?order_id='+row.id;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                                 {
                                    name: 'done', 
                                    text: '完成', 
                                    title: '完成', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-info btn-ajax', 
                                    confirm: '确认更改为已完成吗？', 
                                    url: 'order/done',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                                {
                                    name: 'detail', 
                                    text: '详情', 
                                    title: '订单详情', 
                                    icon: 'fa fa-eye', 
                                    classname: 'btn btn-xs btn-success btn-dialog', 
                                    url: 'order/detail',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                            
                                if(row.pay_status != 3) {
                                    $(table).data("operate-refund", null);
                                }
                                if(row.status != 1) {
                                    $(table).data("operate-shipping", null);
                                }
                                if(row.status != 2 && row.status != 5) {
                                    $(table).data("operate-done", null);
                                }
                                $(table).data("operate-del", null);
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});